package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.telecom.cms.dao.LOC_LATNDAO;
import com.telecom.cms.dao.SEG_BIZ_ATOM_DEFDAO;
import com.telecom.cms.dbconnect.Chinese;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dto.*;



/**
 * @author yueyingnishang
 * 
 */
public class RsrecordAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("JISAutoDetect");
		String[] atomname = request.getParameterValues("type");
		List atomlist = new ArrayList();
		Connection con = GetConnect.getCsConnection();
		SEG_BIZ_ATOM_DEFDAO dao = new SEG_BIZ_ATOM_DEFDAO(con);
		String LatnId = request.getSession().getAttribute("latnid").toString();

		if (null == request.getSession().getAttribute("AtomList")) {
			if (atomname != null) {
				for (int i = 0; i < atomname.length; i++) {
					String chinesevalue = "";
					String value = "";
					String[] atomvalue = request
							.getParameterValues(atomname[i]);
					// 如果是省公司选了本地网，则将其切换到那个本地网
					if (LatnId.equals("1000")
							&& Chinese.toChinaRs(atomname[i])
									.equals("地理位置细到支局")) {
						request.getSession().setAttribute("latnid",
								atomvalue[0]);

						request.getSession().setAttribute("User_Auth", "普通用户");
						LOC_LATNDTO dto = new LOC_LATNDAO()
								.findById(atomvalue[0]);
						String latncd = dto.getLatn_cd();
						String latndesc = dto.getLatn_desc();
						request.getSession().setAttribute("latndesc", latndesc);
						request.getSession().setAttribute("latncd", latncd);
						request.setAttribute("id", "3");
						return mapping.findForward("bureau");
					}

					SEG_BIZ_ATOM_DEFDTO atomdto = dao.findByName(Chinese
							.toChinaRs(atomname[i]));

					if (atomvalue != null
							&& atomdto != null
							&& atomdto.getBIZ_ATOM_FLAG().equalsIgnoreCase(
									"other")) {

						String sqlvalue = "";
						for (int j = 0; j < atomvalue.length; j++) {

							value = value + atomvalue[j] + ",";
							if (j == 0) {

								sqlvalue = sqlvalue + "'" + atomvalue[j] + "'";

							} else {

								sqlvalue = sqlvalue + ",'" + atomvalue[j] + "'";

							}

						}
						if (atomdto.getBIZ_ATOM_NAME().equals("用户自定义查询")
								|| 1 == atomdto.getSHOW_TYPE_ID()) {
							chinesevalue = chinesevalue
									+ value
											.substring(0, value
													.lastIndexOf(","));
							ShowatomDTO dto = new ShowatomDTO(Chinese
									.toChinaRs(atomname[i]), value, atomdto
									.getBIZ_ATOM_ID(), chinesevalue);
							atomlist.add(dto);
						} else {
							PreparedStatement psmt = con
									.prepareStatement(" select "
											+ atomdto.getSRC_TABLE_COL_NAME()
											+ " from "
											+ atomdto.getCDE_SRC_TABLE_NAME()
											+ " where "
											+ atomdto.getBAS_TABLE_COL_NAME()
											+ " in(" + sqlvalue + ")");

							ResultSet rset = psmt.executeQuery();

							while (rset.next()) {

								chinesevalue = chinesevalue + rset.getString(1)
										+ ",";
							}

							if (rset != null) {

								rset.close();

							}

							if (psmt != null) {

								psmt.close();

							}

							ShowatomDTO dto = new ShowatomDTO(Chinese
									.toChinaRs(atomname[i]), Chinese
									.toChinaRs(value),
									atomdto.getBIZ_ATOM_ID(), chinesevalue);
							atomlist.add(dto);
						}
					} else if (atomvalue == null) {

						ShowatomDTO dto = new ShowatomDTO(Chinese
								.toChinaRs(atomname[i]), null, atomdto
								.getBIZ_ATOM_ID(), chinesevalue);
						atomlist.add(dto);
					} else {

						List personallist = new ArrayList();
						for (int j = 0; j < atomvalue.length; j++) {
							if (atomvalue[j].indexOf("－") != -1
									&& atomvalue[j].indexOf("－") != 0) {

								value = value + atomvalue[j] + ",";

							} else if (atomvalue[j] != ""
									&& atomvalue[j] != null) {

								personallist.add(atomvalue[j]);
							}
						}
						String personalvalue = null;
						if ("money".equalsIgnoreCase(atomdto.getBIZ_ATOM_FLAG()) && personallist.size() == 2) {
							String personalString1 = (String) personallist.get(0);
							float personalvalue1 = Float.parseFloat(personalString1);
							String personalString2 = (String) personallist.get(1);
							float personalvalue2 = Float.parseFloat(personalString2);
							if (personalvalue1 < personalvalue2) {
								personalvalue = personalvalue1 + "－" + personalvalue2;
							} 
							else {
								personalvalue = personalvalue2 + "－" + personalvalue1;
							}
						} 
						else if (atomdto.getBIZ_ATOM_FLAG().equalsIgnoreCase("time") && personallist.size() == 2) {
							String personalString1 = (String) personallist.get(0);
							float personalvalue1 = Float.parseFloat(personalString1);
							String personalString2 = (String) personallist.get(1);
							float personalvalue2 = Float.parseFloat(personalString2);
							if (personalvalue1 < personalvalue2) {
								personalvalue = personalvalue1 + "－" + personalvalue2;
							} 
							else {
								personalvalue = personalvalue2 + "－" + personalvalue1;
							}
						}

						if (personalvalue != null) {
							value = value + personalvalue;
						}
						ShowatomDTO dto = new ShowatomDTO(Chinese
								.toChinaRs(atomname[i]), Chinese
								.toChinaRs(value), atomdto.getBIZ_ATOM_ID(),
								Chinese.toChinaRs(value));
						atomlist.add(dto);

					}
				}
			}
		} else {

			atomlist = (List) request.getSession().getAttribute("AtomList");
			if (atomname != null) {
				for (int i = 0; i < atomname.length; i++) {

					SEG_BIZ_ATOM_DEFDTO atomdto = dao.findByName(Chinese
							.toChinaRs(atomname[i]));

					String name = Chinese.toChinaRs(atomname[i]);
					for (int j = 0; j < atomlist.size(); j++) {

						ShowatomDTO dto = (ShowatomDTO) atomlist.get(j);
						String atom = dto.getAtomname();
						if (atom.equalsIgnoreCase(name)) {

							atomlist.remove(j);
						}
					}
					String chinesevalue = "";
					String value = "";
					String[] atomvalue = request
							.getParameterValues(atomname[i]);

					// 如果是省公司选了本地网，则将其切换到那个本地网
					if (LatnId.equals("1000")
							&& Chinese.toChinaRs(atomname[i])
									.equals("地理位置细到支局")) {
						request.getSession().setAttribute("latnid",
								atomvalue[0]);

						request.getSession().setAttribute("User_Auth", "普通用户");
						LOC_LATNDTO dto = new LOC_LATNDAO()
								.findById(atomvalue[0]);
						String latncd = dto.getLatn_cd();
						String latndesc = dto.getLatn_desc();
						request.getSession().setAttribute("latndesc", latndesc);
						request.getSession().setAttribute("latncd", latncd);
						request.setAttribute("id", "3");
						return mapping.findForward("bureau");
					}

					if (atomvalue != null && "other".equalsIgnoreCase(atomdto.getBIZ_ATOM_FLAG())) {
						String sqlvalue = "";
						for (int j = 0; j < atomvalue.length; j++) {
							if (atomdto.getSHOW_TYPE_ID() != 100001 || (atomdto.getSHOW_TYPE_ID() == 100001 && atomvalue[j].indexOf("－") != -1))
								value = value + atomvalue[j] + ",";
							if (j == 0) {
								sqlvalue = sqlvalue + "'" + atomvalue[j] + "'";
							} 
							else {
								sqlvalue = sqlvalue + ",'" + atomvalue[j] + "'";
							}
						}
						if (name.equals("用户自定义查询")
								|| 1 == atomdto.getSHOW_TYPE_ID()) {
							chinesevalue = chinesevalue
									+ value
											.substring(0, value
													.lastIndexOf(","));
							ShowatomDTO dto = new ShowatomDTO(Chinese
									.toChinaRs(atomname[i]), value, atomdto
									.getBIZ_ATOM_ID(), chinesevalue);
							atomlist.add(dto);
						} else {
							if (atomdto.getSRC_TABLE_COL_NAME() != null && !atomdto.getSRC_TABLE_COL_NAME().trim().equals("")){
								PreparedStatement psmt = con.prepareStatement(" select "
												+ atomdto.getSRC_TABLE_COL_NAME()
												+ " from "
												+ atomdto.getCDE_SRC_TABLE_NAME()
												+ " where "
												+ atomdto.getBAS_TABLE_COL_NAME()
												+ " in(" + sqlvalue + ")");
								ResultSet rset = psmt.executeQuery();
								while (rset.next()) {
									chinesevalue = chinesevalue + rset.getString(1) + ",";
								}
								if (rset != null) {
									rset.close();
								}
								if (psmt != null) {
									psmt.close();
								}
								ShowatomDTO dto = new ShowatomDTO(Chinese
										.toChinaRs(atomname[i]), Chinese
										.toChinaRs(value),
										atomdto.getBIZ_ATOM_ID(), chinesevalue);
								atomlist.add(dto);
							}
							else{
								ShowatomDTO dto = new ShowatomDTO(Chinese
										.toChinaRs(atomname[i]), Chinese
										.toChinaRs(value),
										atomdto.getBIZ_ATOM_ID(), value);
								atomlist.add(dto);
							}
						}
					} 
					else if (atomvalue == null) {
						ShowatomDTO dto = new ShowatomDTO(Chinese
								.toChinaRs(atomname[i]), null, atomdto
								.getBIZ_ATOM_ID(), chinesevalue);
						atomlist.add(dto);
					} 
					else {
						List personallist = new ArrayList();
						for (int j = 0; j < atomvalue.length; j++) {
							if (atomvalue[j].indexOf("－") != -1) {
								value = value + atomvalue[j] + ",";
							} else if (atomvalue[j] != ""
									&& atomvalue[j] != null) {
								personallist.add(atomvalue[j]);
							}
						}
						String personalvalue = null;
						if ("money".equalsIgnoreCase(atomdto.getBIZ_ATOM_FLAG()) && personallist.size() == 2) {
							String personalString1 = (String) personallist.get(0);
							float personalvalue1 = Float.parseFloat(personalString1);
							String personalString2 = (String) personallist.get(1);
							float personalvalue2 = Float.parseFloat(personalString2);
							if (personalvalue1 < personalvalue2) {
								personalvalue = personalvalue1 + "－" + personalvalue2;
							} 
							else {
								personalvalue = personalvalue2 + "－" + personalvalue1;
							}
						} 
						else if (atomdto.getBIZ_ATOM_FLAG().equalsIgnoreCase("time") && personallist.size() == 2) {
							String personalString1 = (String) personallist.get(0);
							float personalvalue1 = Float.parseFloat(personalString1);
							String personalString2 = (String) personallist.get(1);
							float personalvalue2 = Float.parseFloat(personalString2);
							if (personalvalue1 < personalvalue2) {
								personalvalue = personalvalue1 + "－" + personalvalue2;
							} 
							else {
								personalvalue = personalvalue2 + "－" + personalvalue1;
							}
						}

						if (personalvalue != null) {
							value = value + personalvalue;
						}
						ShowatomDTO dto = new ShowatomDTO(Chinese
								.toChinaRs(atomname[i]), Chinese
								.toChinaRs(value), atomdto.getBIZ_ATOM_ID(),
								Chinese.toChinaRs(value));
						atomlist.add(dto);

					}

				}

			}

		}
		// if an atom are null then remove the same name atom in the session
		String removename = request.getParameter("remove");
		for (int i = 0; i < atomlist.size(); i++) {
			ShowatomDTO removedto = (ShowatomDTO) atomlist.get(i);
			if (removedto.getAtomname().equals(removename)) {
				atomlist.remove(removedto);
			}
		}
		request.getSession().setAttribute("AtomList", atomlist);

		List listbiz = (List) request.getSession().getAttribute("Outputlist");
		List conditionAtoms = atomlist;
		if (conditionAtoms != null) {
			String copycondition = (String)request.getSession().getAttribute("copycondition");
			SEG_BIZ_ATOM_DEFDAO bizdao = new SEG_BIZ_ATOM_DEFDAO(con);
			Iterator cit = conditionAtoms.iterator();
			if ("yes".equals(copycondition)) {
				// insert condition atoms into outputlist
				if (listbiz == null)
					listbiz = new ArrayList();
				while (cit.hasNext()) {
					ShowatomDTO showatom = (ShowatomDTO) cit.next();
					int atomid = showatom.getAtomid();
					String aname = showatom.getAtomname().trim() + "(当前)";
					SEG_BIZ_ATOM_DEFDTO dto = new SEG_BIZ_ATOM_DEFDTO();
					dto = bizdao.findByName(aname);
					if (dto == null) {
						dto = bizdao.findById(atomid);
					}
					if (!listbiz.contains(dto)) {
						listbiz.add(dto);
					}
				}
				for (int i = 0; i < listbiz.size(); i++) {
					SEG_BIZ_ATOM_DEFDTO removedto = (SEG_BIZ_ATOM_DEFDTO) listbiz.get(i);
					String removecopy = removedto.getBIZ_ATOM_NAME();
					if (removename == null)
						continue;
					if (removecopy.equals(removename)||removecopy.equals(removename.trim()+"(当前)")) {
						listbiz.remove(removedto);
					}
				}
			}
		}
		request.getSession().setAttribute("Outputlist", listbiz);
		return mapping.findForward("success");
	}

}
